<template>
  <basic-container>
    <avue-crud :option="moption"
               :table-loading="mloading"
               :data="mdata"
               :page.sync="mpage"
               :permission="mpermissionList"
               :before-open="mbeforeOpen"
               v-model="mform"
               ref="mcrud"
               @row-update="mrowUpdate"
               @row-save="mrowSave"
               @row-del="mrowDel"
               @search-change="msearchChange"
               @search-reset="msearchReset"
               @selection-change="mselectionChange"
               @current-change="mcurrentChange"
               @size-change="msizeChange"
               @refresh-change="mrefreshChange"
               @on-load="monLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.maintain_delete"
                   @click="mhandleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {mgetList,mgetDetail,msave,mremove,mupdate} from "@/api/emer/maintain";
  import {mapGetters} from "vuex";

  export default {
    props:{
      wbjlBox:{default:false},
      eqptId:"",
      zjbysj:""
    },
    data() {
      return {
        mform: {},
        mquery: {},
        mloading: true,
        mpage: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        mselectionList: [],
        moption: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "保养人",
              prop: "byr",
              align:'center',
              rules: [{
                required: true,
                message: "请输入保养人",
                trigger: "blur"
              }]
            },
            {
              label: "保养周期",
              prop: "byzq",
              dataType:"number",
              type:"select",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=emer_byzq",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入保养周期",
                trigger: "blur"
              }]
            },
            {
              label: "保养内容",
              prop: "bynr",
              rules: [{
                required: true,
                message: "请输入保养内容",
                trigger: "blur"
              }]
            },
            {
              label: "备注",
              prop: "bz",
              rules: [{
                required: true,
                message: "请输入备注",
                trigger: "blur"
              }]
            },
          ]
        },
        mdata: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      mpermissionList() {
        return {
          addBtn: this.vaildData(this.permission.maintain_add, false),
          viewBtn: this.vaildData(this.permission.maintain_view, false),
          delBtn: this.vaildData(this.permission.maintain_delete, false),
          editBtn: this.vaildData(this.permission.maintain_edit, false)
        };
      },
      mids() {
        let mids = [];
        this.mselectionList.forEach(ele => {
          mids.push(ele.id);
        });
        return mids.join(",");
      }
    },
    watch:{
      wbjlBox(){
        if(this.wbjlBox === true){
          this.minitData()
        }
      }
    },
    mounted(){
      this.minitData()
    },
    methods: {
      minitData(){
        this.monLoad(this.mpage)

      },
      mrowSave(row, done, loading) {
        msave(row).then(() => {
          this.monLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      mrowUpdate(row, index, done, loading) {
        mupdate(row).then(() => {
          this.monLoad(this.mpage);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      mrowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return mremove(row.id);
          })
          .then(() => {
            this.monLoad(this.mpage);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      mhandleDelete() {
        if (this.mselectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return mremove(this.mids);
          })
          .then(() => {
            this.monLoad(this.mpage);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.mcrud.toggleSelection();
          });
      },
      mbeforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          mgetDetail(this.mform.id).then(res => {
            this.mform = res.data.data;
          });
        }
        done();
      },
      msearchReset() {
        this.mquery = {};
        this.monLoad(this.mpage);
      },
      msearchChange(params, done) {
        this.mquery = params;
        this.mpage.currentPage = 1;
        this.monLoad(this.mpage, params);
        done();
      },
      mselectionChange(list) {
        this.mselectionList = list;
      },
      mselectionClear() {
        this.mselectionList = [];
        this.$refs.mcrud.toggleSelection();
      },
      mcurrentChange(currentPage){
        this.mpage.currentPage = currentPage;
      },
      msizeChange(pageSize){
        this.mpage.pageSize = pageSize;
      },
      mrefreshChange() {
        this.monLoad(this.mpage, this.mquery);
      },
      monLoad(page, params = {}) {
        this.mloading = true;
        params.businessId=this.eqptId

        mgetList(page.currentPage, page.pageSize, Object.assign(params, this.mquery)).then(res => {
          const data = res.data.data;
          this.mpage.total = data.total;
          this.mdata = data.records;
          this.mloading = false;
          this.mselectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
